package com.imay.live.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.imay.base.activity.BaseActivity;
import com.imay.base.util.ViewUtil;
import com.imay.live.adapter.IndicatorFragmentAdapter;
import com.imay.live.analytics.MobclickAgent;
import com.imay.live.cache.UserInfoCache;
import com.imay.live.fragment.RankingFragment;
import com.imay.live.fragment.SendGiftRankingFragment;
import com.imay.live.helper.DialogFragmentHelper;
import com.imay.live.manager.AccountManager;
import com.imay.live.model.RankingModel;
import com.imay.live.model.TabIndicatorModel;
import com.imay.live.proto.Result;
import com.imay.live.proto.helper.ProtoCallBack;
import com.imay.live.proto.helper.ProtoHttpFactory;
import com.imay.live.proto.helper.ProtoRequestHelper;
import com.imay.live.view.indicator.IndicatorViewPager;
import com.imay.live.view.indicator.ScrollIndicatorView;
import com.imay.live.view.indicator.slidebar.ColorBar;
import com.imay.live.view.indicator.transition.OnTransitionTextListener;
import com.imay.live.R;

import java.util.ArrayList;
import java.util.List;

import static com.imay.live.proto.ResultCode.CodeSuccess;

/**
 * create by nick
 * 个人资料的排行榜
 */
public class ProfileRankingActivity extends BaseActivity implements ProtoCallBack {

    public static final String UID_TAG = "tag_uid";
    private ScrollIndicatorView siv_ranking;
    private ViewPager vp_ranking;
    private long uid;
    private ArrayList<Fragment> fragmentList;
    private String getGiftCommand;
    private int count=100;
    private int start;

    public static void start(Context context,long uid) {
        Intent starter = new Intent(context, ProfileRankingActivity.class);
        starter.putExtra(UID_TAG,uid);
        context.startActivity(starter);
    }


    @Override
    protected int getLayoutId() {
        return R.layout.activity_profile_ranking;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        vp_ranking = $(R.id.vp_ranking);
        siv_ranking = $(R.id.siv_ranking);
        uid = getIntent().getLongExtra(UID_TAG,0);
        setTitle(getString(R.string.mine_ranking_list));
        bindFragments();
        loadData();


    }

    private boolean isMine(){
        return uid==0;
    }

    private void bindFragments() {
        List<TabIndicatorModel> mTabModelList = new ArrayList<>(2);
        mTabModelList.add(new TabIndicatorModel(getString(R.string.profile_ranking_gift_get), false));
        mTabModelList.add(new TabIndicatorModel(getString(R.string.profile_ranking_gift_send), false));
        fragmentList = new ArrayList<>(2);
        fragmentList.add(RankingFragment.newInstance(RankingFragment.TYPE_RANKING));
        if (isMine()) {
            fragmentList.add(SendGiftRankingFragment.newInstance());
        }
        vp_ranking.setOffscreenPageLimit(1);

        ColorBar colorBar =  new ColorBar(this, this.getResources().getColor(R.color.indicator_bar_select), this.getResources().getDimensionPixelOffset(R.dimen.indicator_height));
        siv_ranking.setScrollBar(colorBar);
        colorBar.setWidth((int) ViewUtil.dip2px(this,25f));


        siv_ranking.setOnTransitionListener(new OnTransitionTextListener().setColorId(this, R.color.indicator_text_select, R.color.indicator_text_unselect));
        IndicatorViewPager indicatorViewPager = new IndicatorViewPager(siv_ranking, vp_ranking);
        indicatorViewPager.setAdapter(new IndicatorFragmentAdapter(this,getSupportFragmentManager(),fragmentList,mTabModelList) {
        });
        siv_ranking.setVisibility(isMine()?View.VISIBLE:View.GONE);
        indicatorViewPager.setOnIndicatorPageChangeListener(new IndicatorViewPager.OnIndicatorPageChangeListener() {
            @Override
            public void onIndicatorPageChange(int preItem, int currentItem) {
                if (currentItem == 0) {
                    MobclickAgent.onEvent(ProfileRankingActivity.this, "gift_click_get");
                } else {
                    MobclickAgent.onEvent(ProfileRankingActivity.this, "gift_click_give");
                }
            }
        });
    }

    private void loadData(){
        DialogFragmentHelper.showLoadingDialog(getSupportFragmentManager(),"");
        getGiftCommand = ProtoRequestHelper.task(ProtoHttpFactory.getProfileGetGiftRanking(uid,count,start)).setTag(this.hashCode()).request(this);

    }

    @Override
    public void onRequestSuccess(String command, String data) {
        DialogFragmentHelper.closeLoadingDialog(getSupportFragmentManager());
        if (command.equals(getGiftCommand)) {
            Result<List<RankingModel>> result = JSON.parseObject(data, new TypeReference<Result<List<RankingModel>>>() {}.getType());
            if (result.result == CodeSuccess) {
                ((RankingFragment) fragmentList.get(0)).setRankingData(result.data);
            }
        }
    }


    @Override
    public void onRequestError(String command, int code, String message) {

    }
}
